import { routerRedux } from 'dva/router';
import api from '../api';
import { message } from 'antd'

export default {
    namespace: 'holdingAnalysis',
    state: {
        lPortId: null,// 产品组合ID 默认值 产品ID排序第一的产品 || query中的 lPortId
        cBenchCode: null,// 业绩基准ID 默认值 沪深300 || query中的 cBenchCode
        ratioBegindate: null, // 分类占比 开始时间 null
        ratioEnddate: null, // 分类占比 结束时间 null
        industryID: '38_1', // 分类占比 行业id	null
        trendBegindate: null, // 仓位走势 开始时间 null
        trendEnddate: null, // 仓位走势 结束时间  null
        portfolioAssets: {},// 组合资产 默认值 {} 
        categoryAssets: {},// 类别资产 默认值 {} 
        industryDistribution: {},//行业分布 默认值 {} 
        trend: [],//仓位走势 默认值{} 
        topTenStocks: {} //前十大重仓骨片 默认值{} 
    },
    subscriptions: {
        setup({ dispatch, history }) {
            history.listen(({ pathname, query }) => {
                // 没有参数时，向后端请求数据，并且改变query
                if (pathname === '/customerService/combinatorialAnalysis/holdingAnalysis') {
                    dispatch({ type: 'init', payload: query })
                }
            })
        }
    },
    effects: {
        * init({
            payload
        }, { call, put, select }) {
            // 初始化
            const state = yield select(state => state.holdingAnalysis)
            // 看本次的参数和上次的参数是否一致：
            // 一致的话，不请求，不一致的话，正常请求
            // 定义一个对象用于定义参数
            const newObj = {}
            for (var key in payload) {
                newObj[key] = state[key]
            }
            if (!Object.getOwnPropertyNames(payload).length || JSON.stringify(payload) != JSON.stringify(newObj)) {
                yield put({
                    type: 'setState',
                    payload,
                })
                const state=yield select(state => state.holdingAnalysis)
                if(payload.lPortId&&!state.beginDate){
                    yield put({
                        type: 'getPositionInit', payload
                    })
                }
            }
        },


        * getPositionInit({
            payload
        }, { call, put, select }) {

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)


            //获取持仓分析初始化数据
            const res = yield call(api.position.getPositionInit, query)
            if (res.code === 0) {
                const init = res.data
                init.ratioBegindate=init.beginDate
                init.ratioEnddate=init.endDate
                init.trendBegindate=init.beginDate
                init.trendEnddate=init.endDate
                yield put({
                    type: 'setState', payload: init
                })
            } else {
                message.info('获取持仓分析初始化数据失败！')
            }
        },


        * getPortfolioAssets({
            payload
        }, { call, put, select }) {

            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate)


            //获取组合资产数据  
            const res = yield call(api.position.getPortfolioAssets, query)
            if (res.code === 0) {
                const portfolioAssets = res.data
                yield put({
                    type: 'setState', payload: {
                        portfolioAssets,
                    }
                })
            } else {
                message.info('获取组合资产数据失败！')
            }
        },

        * getCategoryAssets({
            payload
        }, { call, put, select }) {
            //获取类别资产数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate)

            const res = yield call(api.position.getCategoryAssets, query)
            if (res.code === 0) {
                const categoryAssets = res.data
                yield put({
                    type: 'setState', payload: {
                        categoryAssets
                    }
                })
            } else {
                message.info('获取列别资产数据失败！')
            }
        },

        * getIndustryDistribution({
            payload
        }, { call, put, select }) {
            //获取行业分布数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.ratioBegindate && (query.begindate = payload.ratioBegindate)
            payload.ratioEnddate && (query.enddate = payload.ratioEnddate)
            payload.industryID && (query.indunum = payload.industryID)

            const res = yield call(api.position.getIndustryDistribution, query)
            if (res.code === 0) {
                const industryDistribution = res.data
                // industryDistribution.list.map((item,i)=>{
                //     item.id=i
                // })
                yield put({
                    type: 'setState', payload: {
                        industryDistribution
                    }
                })
            } else {
                message.info('获取行业分布数据失败！')
            }
        },

        * getPositionTrend({
            payload
        }, { call, put, select }) {
            //获取仓位走势数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.cBenchCode && (query.cBenchCode = payload.cBenchCode)
            payload.trendBegindate && (query.begindate = payload.trendBegindate)
            payload.trendEnddate && (query.enddate = payload.trendEnddate)

            const res = yield call(api.position.getPositionTrend, query)
            if (res.code === 0) {
                const trend = res.data
                yield put({
                    type: 'setState', payload: {
                        trend
                    }
                })
            } else {
                message.info('获取仓位走势数据失败！')
            }
        },

        * getTopTenStocks({
            payload
        }, { call, put, select }) {
            //获取前十大重仓股票数据
            const query = {}
            payload.lPortId && (query.lPortId = payload.lPortId)
            payload.trendBegindate && (query.begindate = payload.trendBegindate)
            payload.trendEnddate && (query.enddate = payload.trendEnddate)
            

            const res = yield call(api.position.getTopTenStocks, query)
            if (res.code === 0) {
                const topTenStocks = res.data
                yield put({
                    type: 'setState', payload: {
                        topTenStocks
                    }
                })
            } else {
                message.info('获取前十大重仓股票数据失败！')
            }
        },

    },
    reducers: {
        setState(state, { payload }) {
            return {
                ...state,
                ...payload
            }
        }
    }
}
